package com.efast.dmom.mgmt.controller.les.pda;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.bean.les.GoodsReceiptBean;
import com.efast.dmom.mgmt.entity.les.TwDocAsnDetails;
import com.efast.dmom.mgmt.entity.les.TwDocAsnHeader;
import com.efast.dmom.mgmt.service.les.TwDocAsnDetailsService;
import com.efast.dmom.mgmt.service.les.TwDocAsnHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

/**
 * @author chlp
 */
@Controller
@RequestMapping(value = "/pdaTwDocAsnHeader")
@Api(description = "Pda->收货->整单收货")
public class PdaTwDocAsnHeaderController {

	private final TwDocAsnHeaderService twDocAsnHeaderService;
	private final TwDocAsnDetailsService twDocAsnDetailsService;

	@Autowired
	public PdaTwDocAsnHeaderController(TwDocAsnHeaderService twDocAsnHeaderService,
			TwDocAsnDetailsService twDocAsnDetailsService) {
		this.twDocAsnHeaderService = twDocAsnHeaderService;
		this.twDocAsnDetailsService = twDocAsnDetailsService;
	}

	/**
	 * 【确认收货】 HEADER的接收:ASN HEADER的接收[ASN状态改为2(即收货完成),收货完成后在TW_INBOUND_ALLOC_DETAIL创建数据]
	 * 
	 * @param twDocAsnHeader
	 *            {"asnSheetNo":"xxx"}
	 */
	@RequestMapping(value = "/headerReceive", method = RequestMethod.POST)
	@ApiOperation(value = "【确认收货】", notes = "请求参数：{\"asnSheetNo\":\"xxx\"}")
	public @ResponseBody ResultBean headerReceive(@RequestBody TwDocAsnHeader twDocAsnHeader) {
		ResultBean result = new ResultBean(true, "'");
		TwDocAsnHeader twDocAsnHeader1 = twDocAsnHeaderService.selectTwDocAsnHeaderByObj(twDocAsnHeader);
		List<TwDocAsnDetails> listTwDocAsnDetails = twDocAsnDetailsService.selectTwDocAsnDetails(
				new Assist(Assist.andEq("tw_doc_asn_header_id", twDocAsnHeader1.getTwDocAsnHeaderId())));
		GoodsReceiptBean goodsreceiptbean = new GoodsReceiptBean();
		goodsreceiptbean.setTwDocAsnHeader(twDocAsnHeader1);
		goodsreceiptbean.setListTwDocAsnDetails(listTwDocAsnDetails);
		twDocAsnHeaderService.headerReceive(goodsreceiptbean);
		return result;
	}

	/**
	 * HEADER的的拒绝:[ASN状态改为3(即收货拒绝)]
	 * 
	 * @param twDocAsnHeader
	 *            twDocAsnHeader
	 */
	@ApiOperation(value = "【拒绝】", notes = "请求参数：{\"twDocAsnHeaderId\":\"xxx\"}")
	@RequestMapping(value = "/refuseReceive", method = RequestMethod.POST)
	public @ResponseBody ResultBean refuseReceive(@RequestBody TwDocAsnHeader twDocAsnHeader) {
		ResultBean result = new ResultBean(true, "'");
		System.out.println();
		// ASN单证状态（新单0、部分收货1、收货完成2、拒绝收货3、收货取消4、单证关闭5）
		Assist assist = new Assist();
		twDocAsnHeader.setAsnSheetStatus("3");
		twDocAsnHeaderService.refuseReceive(twDocAsnHeader, assist);
		return result;
	}

	/**
	 * 拒绝的查询接口
	 *
	 * @param twDocAsnHeader
	 *            twDocAsnHeader
	 */
	@RequestMapping(value = "/queryRefuse", method = RequestMethod.POST)
	@ApiOperation(value = "【拒绝的查询接口】", notes = "请求参数：{\"asnSheetNo\":\"xxx\"}")
	public @ResponseBody ResultBean queryRefuse(@RequestBody TwDocAsnHeader twDocAsnHeader) {
		ResultBean result = new ResultBean(true, "'");
		List<TwDocAsnHeader> twDocAsnHeaderList = twDocAsnHeaderService.selectTwDocAsnHeader(
				new Assist(Assist.andEq("asn_sheet_no", twDocAsnHeader.getAsnSheetNo())));
		if (twDocAsnHeaderList.size() == 0) {
			throw new ServiceException("没有查询到数据");
		}
		result.setObject(twDocAsnHeaderList.get(0));
		return result;
	}

	/**
	 * 查询数据
	 *
	 * @param twDocAsnHeader
	 *            twDocAsnHeader
	 */
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	public @ResponseBody ResultBean query(@RequestBody TwDocAsnHeader twDocAsnHeader) {
		ResultBean result = new ResultBean(true, "'");
		// 可能是asn单号，也可能是序列号
		TwDocAsnHeader twDocAsnHeader1 = twDocAsnHeaderService.selectTwDocAsnHeaderByObj(twDocAsnHeader);
		List<TwDocAsnDetails> listTwDocAsnDetails = twDocAsnDetailsService.selectTwDocAsnDetails(
				new Assist(Assist.andEq("tw_doc_asn_header_id", twDocAsnHeader1.getTwDocAsnHeaderId())));
		result.setObject(listTwDocAsnDetails);
		return result;
	}

}
